package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseDescriptionMapper;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author qing
 * @since 2021-08-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionService courseDescriptionService;

    @Resource
    private EduVideoService eduVideoService;//小节

    @Resource
    private EduChapterService eduChapterService; //章节




    //添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        if (courseInfoVo.getTitle() == null || courseInfoVo.getTitle() == ""){
            throw new GuliException(20001,"标题名不能为空");
        }



        //1.向课程表添加课程基本信息
        //CourseInfoVo转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        //insert大于0添加成功了，否则就失败了
        if (insert <= 0){
            //添加失败
            throw  new GuliException(20001,"添加课程失败");
        }else {
            //获取课程id
            String cid = eduCourse.getId();

            //2.向课程简介表添加课程简介
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            eduCourseDescription.setId(cid);//描述id = 课程 id （一对一关系）
            eduCourseDescription.setDescription(courseInfoVo.getDescription());
            courseDescriptionService.save(eduCourseDescription);

            return cid;
        }

    }

    /**
     * 根据课程id查询课程基本信息
     * @param courserId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courserId) {

        //1.先查下课程表
        EduCourse eduCourse = baseMapper.selectById(courserId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //封装数据
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2.查询课程描述
        EduCourseDescription courseDescription = courseDescriptionService.getById(courserId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    /**
     * 修改课程信息
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {

        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        //1.修改课程表
        int i = baseMapper.updateById(eduCourse);
        if (i == 0 ){
            throw new GuliException(20001,"修改课程失败");
        }

        //2.修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        boolean b = courseDescriptionService.updateById(courseDescription);
        if (!b){
            throw new GuliException(20001,"修改课程描述失败");
        }

    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    /**
     * 删除课程信息
     * @param courseId
     */
    @Override
    public void removeCourse(String courseId) {

        //1.根据课程id 删除课程小节
        eduVideoService.removeVideoByCourseId(courseId);

        //2.根据课程id 删除章节
        eduChapterService.removeChapterByCorseId(courseId);

        //3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        //4.根据课程id删除课程本身
        int i = baseMapper.deleteById(courseId);
        if (i == 0 ){
            throw new GuliException(20001,"删除失败");
        }


    }

    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> eduCoursePage, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        //判断条件值是否为空，如果不为空拼接条件

        //判断一级分类是否存在
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            eduCourseQueryWrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        //判断二级分类是否存在
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            eduCourseQueryWrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }

        //判断排序参数是否存在
        //关注度
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            eduCourseQueryWrapper.orderByDesc("buy_count");
        }
        //最新时间排序
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            eduCourseQueryWrapper.orderByDesc("gmt_create");
        }
        //价格排序
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            eduCourseQueryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(eduCoursePage,eduCourseQueryWrapper);

        List<EduCourse> records = eduCoursePage.getRecords();
        long current = eduCoursePage.getCurrent();
        long pages = eduCoursePage.getPages();
        long size = eduCoursePage.getSize();
        long total = eduCoursePage.getTotal();

        //是否有下一页
        boolean hasNext = eduCoursePage.hasNext();
        //是否有上一页
        boolean hasPrevious = eduCoursePage.hasPrevious();


        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;

    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
